package run.servlet.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.RegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.EventListener;

@Configuration
public class WebConfig {
    @Slf4j
    public static class CustomWebListener implements ServletContextListener , EventListener {
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            log.info("CustomWebListener init...");
            ServletContextListener.super.contextInitialized(sce);
        }
    }
    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean(){
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
        servletListenerRegistrationBean.setListener(new CustomWebListener());
        return servletListenerRegistrationBean;
    }
    @Slf4j
    @javax.servlet.annotation.WebListener
    public static class WebListener implements ServletContextListener {
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            log.info("WebListener init by the Annotation Scan...");
            ServletContextListener.super.contextInitialized(sce);
        }

        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            ServletContextListener.super.contextDestroyed(sce);
        }
    }
}
